package com.jiatuxueyuan.the4.remoteio;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class InboundProcessor1 extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
        super.channelRegistered(ctx);
    }

    @Override
    public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
        super.channelUnregistered(ctx);
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        super.channelActive(ctx);
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        super.channelInactive(ctx);
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        //在这里接受到Netty帮我们从socket中读取到的字节流数据，一般来说msg就是ByteBuf对象，如果你在当前Handler之前又放了一些字节转对象的Handler，
        //那么这里的msg就是你自己定义的POJO类，也是比较方便的

        //1. 从buf中读取到数据
        ByteBuf buf = (ByteBuf) msg;
        //2. 执行业务逻辑，然后在再把结果发出去
        Object result = null;//TODO,调用业务逻辑
        ctx.writeAndFlush(result);

        //3. 如果当前Handler是Inbound的最后一个Handler，下面这行代码就可以注释掉了，调用了也没啥用，因为它的后面已经没有InboundHandler了
        // super里面就是ctx.fireChannelRead(msg); 必须调用fire方法，才会让read事件往下一个InboundHandler传播（如果还有下一个的话，如果没有就到此为止）
        super.channelRead(ctx, msg);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        super.channelReadComplete(ctx);
    }

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        super.userEventTriggered(ctx, evt);
    }

    @Override
    public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
        super.channelWritabilityChanged(ctx);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        super.exceptionCaught(ctx, cause);
    }

    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        super.handlerAdded(ctx);
    }

    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
        super.handlerRemoved(ctx);
    }
}
